package com.hshh.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hshh.dao.CompanyDAO;
import com.hshh.service.CompanyUploadService;

@Service
public class CompanyUploadServiceImpl implements CompanyUploadService {

	@Autowired
	private CompanyDAO companyDAO;

	@Override
	public List<Map<String, Object>> insertCompanies(List<Map<String, Object>> param) {
		// TODO Auto-generated method stub		
		for(int i = 0 ; i < param.size() ; i++){
			try{
				Map<String, Object> convertMap = this.convertExcelToCompany(param.get(i));
				
				List<Map<String, Object>> existList = companyDAO.selectExistCompany(convertMap);
				
				//중복 체크(코드  : Duplication)
				if(existList != null && existList.size() > 0){
					param.get(i).put("result", "D");
				}
				//회사 저장 성공(코드  : True)
				else if(companyDAO.insertCompany(convertMap) == 1){
					param.get(i).put("result", "T");
				}
				//회사 저장 실패 (코드 : False)
				else{
					param.get(i).put("result", "F");
				}
			} catch (Exception e) {
				// TODO: handle exception
				param.get(i).put("result", "F");
				e.printStackTrace();
			}
		}
		return param;
	}
	
	private Map<String, Object> convertExcelToCompany(Map<String, Object> param){
		String[] columns = { "businessDivision", "companyNumber", "companyName",
							"companyPresident", "businessAddr", "businessType",
							"businessConditions" };
		Map<String, Object> result = new HashMap<String, Object>();
		for(int i = 0 ; i < columns.length ; i++){
			if(param.containsKey("excel_" + i)){
				result.put(columns[i], param.get("excel_" + i));
			}
			else{
				result.put(columns[i], null);
			}
		}
		return result;
	}
	
}